export default {
  namespaced: true,
  state: () => ({
    address: JSON.parse(uni.getStorageSync("address") || "{}"),
    token: uni.getStorageSync("token") || "",
    userinfo: JSON.parse(uni.getStorageSync("userinfo") || "{}"),
  }),
  mutations: {
    // 1. 保存用户选择的收货地址
    updateAddress(state,address){
      state.address = address
      // 进行持久化保存
      this.commit("m_user/saveAddressToStorage")
    },
    // 2. 保存用户信息
    updateUserInfo(state,userinfo){
      state.userinfo = userinfo
      // 进行持久化保存
      this.commit("m_user/saveUserinfoToStorage")
    },
    // 3. 保存token值
    updateToken(state,token){
      state.token = token
      // 进行持久化保存
      this.commit("m_user/saveTokenToStorage")
    },
    // 持久化保存token值
    saveTokenToStorage(state){
      uni.setStorageSync("token",state.token)
    },
    // 持久化保存地址
    saveUserinfoToStorage(state){
      uni.setStorageSync("userinfo",JSON.stringify(state.userinfo))
    },
    // 持久化保存地址
    saveAddressToStorage(state){
      uni.setStorageSync("address",JSON.stringify(state.address))
    }
  },
  getters:{
    // 获取地址信息
    addStr(state){
       if(!state.address.provinceName) return 
       const addr = state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
       return addr
    }
  }
}